home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Problem Negating an Unsigned Char
- Date: 03 Mar 1996 05:21:31 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Mar2222131@qcd.lanl.gov>
- References: <Dnnros.Lq.0.-s@hkusuc.hku.hk>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: h8716718@hkusua.hku.hk's message of Sat, 2 Mar 1996 21:00:27 GMT
-
- In article <Dnnros.Lq.0.-s@hkusuc.hku.hk>
- h8716718@hkusua.hku.hk (Starry Hung) writes:
- <snip>
- SH: coding in a little bit redundant way (add a casting), however, I can't
- SH: convince myself why the language performs like this, should anybody
- SH: kindly tell me the story?
- <snip>
- SH: unsigned char a=0x11;
- SH: unsigned char b=0xEE;
- <snip>
- SH: void main( void ) {
-
- Declaring main as returning void invokes undefined behaviour. This
- means that what the compiler does with your program cannot be
- explained according to the rules of the C language. I assume that you
- had actually written `int main(void) {' instead.
-
- SH: if( a == ~b ) {
-
- The problem is that before the application of almost any arithmetic or
- bit manipulation operators, `integral promotions' take place. These
- change char and signed and unsigned char and short to either int ot
- unsigned int (depending on how big they are). So, the quantity that ~
- is complementing is not really a char: it is possibly an int (or
- unsigned int), say 0x00EE. The complement of this is then 0xFF11.
-
- On the other hand a is also promoted (before comparison), but to
- 0x0011. Hence the test fails.
-
- SH: c=1;
- SH: }
- SH: }
- SH:
- SH: The c remains unchange, while it changes to 1 if I cast the ~b to unsigned
- SH: char as if( a == (unsigned char) ~b )
-
- In this case, the 0xFF11 (say) gets converted back to an unsigned char
- 0x11, and is then promoted to 0x0011 before comparison.
-
- Hope this helps.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-